#!/usr/bin/env python
#
#       GeneratePyunit.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import shutil

class PyFramework:

    def __init__(self):
        """
        Specification for generated file
        """
        self.tpath="/tmp/PyTestGenerator%d"%os.getpid()
        self.temp_file = file(self.tpath,"a")
        self.counter=001

        self.temp_file.write("###Generated using PyTestGenerator\n#!/usr/bin/env python\n\n#Format\n#test_<Test_Number>_<Entity_Name>[<Arg_Status>]<Predicted_Status>_<Comment>\n\n")
        self.temp_file.write("import unittest\n")
        self.temp_file.write("import sys\n")

    def writeheader(self):
        """
        Write the basic details for the framework
        Class name for the generated file is defaulted to PyUnitframework
        Can be substituted with the desired Class Name
        """
        self.temp_file.write("class PyUnitframework(unittest.TestCase):\n")
        self.temp_file.write("\t'''Test Cases generated for ")

    def writecases(self,gmod,name,argnum,status,arguments=[],comment=None,classmod=None,clargs=None):
        """
        Cases generated based on number of arguments
        """
        if classmod:
            if clargs:
                gmod="%s.%s(%s)"%(classmod,gmod,','.join(clargs))
            else:
                gmod="%s.%s()"%(classmod,gmod)

        if not argnum :
            self.temp_file.write("\tdef test_%d_%s_WithoutArgs_%s_%s(self):\n"\
                %(self.counter,name,status,comment))
            if status == 'Pass' :
                self.temp_file.write("\t\t%s.%s()\n"%(gmod,name))
            elif status == 'Fail' :
                self.temp_file.write\
                    ("\t\tself.assertRaises(Exception,%s.%s)\n"%(gmod,name))
            self.counter = self.counter + 1
        else :
            self.temp_file.write("\tdef test_%d_%s_WithArgs_%s_%s(self):\n"\
                %(self.counter,name,status,comment))
            if status == 'Pass' :
                self.temp_file.write("\t\t%s.%s(%s)\n"\
                    %(gmod,name,','.join(arguments)))
            elif status == 'Fail' :
                self.temp_file.write\
                    ("\t\tself.assertRaises(Exception,%s.%s,%s)\n"\
                    %(gmod,name,','.join(arguments)))
            self.counter = self.counter + 1

    def writefooter(self):
        """
        Write the init section
        The value for the addedtest Suite is defaulted to PyUnitframework
        Modify the value if had u changed the class name
        """
        self.temp_file.write('\nif __name__=="__main__":\n\ttestlist=unittest.TestSuite()\n\ttestlist.addTest(unittest.makeSuite(PyUnitframework))\n\tresult = unittest.TextTestRunner(verbosity=2).run(testlist)\n\tif not result.wasSuccessful():\n\t\tsys.exit(1)\n\tsys.exit(0)')

    def writefile(self,comment):
        """
        Write Comments
        """
        self.temp_file.write(comment)

    def closefile(self,fpath=os.getcwd(),fname=''):
        """
        Generate the cases and save it to the file
        """
        del self.temp_file
        shutil.move(self.tpath,os.path.join(fpath,fname))

